const query = require('./fun')
const sqlCount = 'SQL_CALC_FOUND_ROWS';
const { sqlAddStr, sqlWhereStr, sqlSelectStr } = require('../util/comm/comm')
const { USER, NEWS, SWIPER, COLLECTION, WATCH, COMM, COMMCOLLECTION } = require('./database')
/**
 * 判断是否存在注册用户
 */
function isUser(data) {
    return new Promise((resolve, reject) => {
        query(`select  userId,userName,userUrl,userSex,userIntegral from ${USER} where username=? `, data)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 注册用户
 */
function registerUser(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${USER}(${obj.keys}) VALUES (${obj.vals})`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 *  登录
 */
function isLogin(data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlWhereStr(sql, data)
        query(`select userId,userName,userUrl,userSex,userIntegral  from ${USER} ${sql}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取新闻信息列表
 */
function newsList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select ${sqlCount} ${NEWS}.*from ${NEWS} ${condition} order by newsId desc limit ?,${limit}`, { num }, true)
            .then(async (res) => {
                resolve(res);
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取轮播图
 */
function swiperList() {
    return new Promise((resolve, reject) => {
        query(`select ${sqlCount} * from ${SWIPER} `, {}, true)
            .then(async (res) => {
                resolve(res);
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取新闻详情
 */
function newsInfo(newsId) {
    return new Promise((resolve, reject) => {
        query(`select  * from ${NEWS} where newsId=?`, { newsId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 判断新闻是否点赞
 */
function isaddCollection(data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlWhereStr(sql, data)
        query(`select * from ${COLLECTION} ${sql}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 判断是否点赞
 */
function isaddWatch(data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlWhereStr(sql, data)
        query(`select * from ${WATCH} ${sql}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 新闻点赞
 */
function addNewsCollection(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${COLLECTION}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改新闻点赞数目
 */
function changeNewsCollection(data) {
    return new Promise((resolve, reject) => {
        let sql = `newsCollection=newsCollection+${data.newsCollection}`;
        query(`UPDATE ${NEWS} SET ${sql} WHERE newsId=${data.newsId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 取消新闻点赞
 */
function delNewsCollection(data) {
    let condition = '';
    condition = sqlWhereStr(condition, data);
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${COLLECTION} ${condition}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取新闻信息列表
 */
function commList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select ${sqlCount} ${COMM}.*from ${COMM} ${condition} order by commId desc limit ?,${limit}`, { num }, true)
            .then(async (res) => {
                resolve(res);
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 判断评论是否点赞
 */
function isaddCommCollection(data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlWhereStr(sql, data)
        query(`select * from ${COMMCOLLECTION} ${sql}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 评论点赞
 */
function addCommCollection(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${COMMCOLLECTION}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改评论点赞数目
 */
function changeCommCollection(data) {
    return new Promise((resolve, reject) => {
        let sql = `commCollection=commCollection+${data.commCollection}`;
        query(`UPDATE ${COMM} SET ${sql} WHERE commId=${data.commId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 取消评论点赞
 */
function delCommCollection(data) {
    let condition = '';
    condition = sqlWhereStr(condition, data);
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${COMMCOLLECTION} ${condition}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加评论
 */
function addComment(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${COMM}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取指定评论
 */
function commentId(commId) {
    return new Promise((resolve, reject) => {
        query(`select ${COMM}.*,${USER}.userName,${USER}.userUrl from ${COMM},${USER} where commId=?`, { commId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 领取积分
 */
function addIntegral(data) {
    return new Promise((resolve, reject) => {
        let sql = `userIntegral=userIntegral+${data.userIntegral}`;
        query(`UPDATE ${USER} SET ${sql} WHERE userId=${data.userId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 新闻观看记录
 */
function addWatch(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${WATCH}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改新闻观看记录数目
 */
function changeNewsWatch(data) {
    return new Promise((resolve, reject) => {
        let sql = `newsWatch=newsWatch+${data.newsWatch}`;
        query(`UPDATE ${NEWS} SET ${sql} WHERE newsId=${data.newsId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 用户提交新闻
 */
function userNewsSave(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${NEWS}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
exports.registerUser = registerUser;
exports.isUser = isUser;
exports.isLogin = isLogin;
exports.newsList = newsList;
exports.swiperList = swiperList;
exports.newsInfo = newsInfo;
exports.isaddCollection = isaddCollection;
exports.isaddWatch = isaddWatch;
exports.addNewsCollection = addNewsCollection;
exports.changeNewsCollection = changeNewsCollection;
exports.delNewsCollection = delNewsCollection;
exports.commList = commList;
exports.isaddCommCollection = isaddCommCollection;
exports.addCommCollection = addCommCollection;
exports.changeCommCollection = changeCommCollection;
exports.delCommCollection = delCommCollection;
exports.addComment = addComment;
exports.commentId = commentId;
exports.addIntegral = addIntegral;
exports.addWatch = addWatch;
exports.changeNewsWatch = changeNewsWatch;
exports.userNewsSave=userNewsSave;